package great.easychat.help.dialog;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import great.easychat.help.CustomAppcation;
import great.easychat.help.R;
import great.easychat.help.util.PlatformUtil;
import great.easychat.help.util.ToastUtil;
import great.easychat.help.util.UserInfoManger;

/**
 * Created by SuperMan on 2018/09/18 0018.
 * 这边微信分享使用安卓原生的微信唤起
 * 因为分享sdk不支持多张图片的分享和发送
 * sdk只用来判断是否安装了微信
 */

public class SharePop extends BasePopup implements View.OnClickListener {

    LinearLayout ll_send_wechat;
    LinearLayout ll_share_friend;
    TextView tv_cancel;
    Activity activity;
    String shareMsg;

    public SharePop(Activity context) {
        super(context);
        activity = context;
        ll_send_wechat = view.findViewById(R.id.ll_send_wechat);
        ll_share_friend = view.findViewById(R.id.ll_share_friend);
        tv_cancel = view.findViewById(R.id.tv_cancel);

        ll_send_wechat.setOnClickListener(this);
        ll_share_friend.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
    }

    public void setShareMsg(String shareMsg) {
//        this.shareMsg = shareMsg.replace("n", "\n") + "\n邀请码：" + UserInfoManger.getUserInfo().getInvite_code();
    }

    List<String> datas = new ArrayList<>();

//    public void initData(List<ImageFileBean> imageFileBeans) {
//        datas.clear();
//        for (ImageFileBean data : imageFileBeans) {
//            datas.add(data.getFilePath());
//        }
//    }

    public void initData(String path) {
        datas.clear();
        datas.add(path);
    }

    @Override
    public View getView() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.share_pop, null);
        return view;
    }

    @Override
    public void onClick(View v) {
        if (v == tv_cancel) {
            dismiss();
        } else if (v == ll_send_wechat) {
            if (shareCallback != null) {
                shareCallback.onClickSendToFriend();
            }
            shareWechatFriend(activity, shareMsg);
            dismiss();
        } else if (v == ll_share_friend) {
            if (shareCallback != null) {
                shareCallback.onClickShareTo();
            }
            shareQQ(activity, shareMsg);
            dismiss();
        }
    }

    public void setShareCallback(ShareCallback shareCallback) {
        this.shareCallback = shareCallback;
    }

    ShareCallback shareCallback;

    interface ShareCallback {

        void onClickSendToFriend();

        void onClickShareTo();

    }

    public UMImage getUMImage(String path) {
        UMImage image = new UMImage(mContext, new File(path));//本地文件
        image.setThumb(image);
        return image;
    }

    public ShareAction getShareAction(String msg, SHARE_MEDIA share_media) {
        ShareAction shareAction = new ShareAction(activity);

        UMImage[] umImages = new UMImage[datas.size()];
        for (int i = 0; i < datas.size(); i++) {
            umImages[i] = getUMImage(datas.get(i));
        }
        shareAction.withMedias(getUMImage(datas.get(0)), getUMImage(datas.get(1)));
        shareAction.setPlatform(share_media)
                .withText(msg)
                .setCallback(null).share();
        return shareAction;
    }

    /**
     * 拉起微信发送多张图片给好友
     */
    private void sendToFriend() {
        Intent shareIntent = new Intent();
        //1调用系统分析
        shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        int count = 0;
        if (datas.size() > 9) {
            count = 9;
            ToastUtil.show("一次最多发送9张哦");
        } else {
            count = datas.size();
        }
        //2添加图片数组
        ArrayList<Uri> imageUris = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            imageUris.add(Uri.fromFile(new File(datas.get(i))));
        }
        shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
        shareIntent.setType("image/*");
        //3指定选择微信
        ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
        shareIntent.setComponent(componentName);
        //4开始分享
        activity.startActivity(Intent.createChooser(shareIntent, "分享图片"));
    }

    /**
     * 分享到微信朋友圈
     */
    public void shareFriend() {
        Intent shareIntent = new Intent();
        // 1 Finals 2016-11-2 调用系统分享
        shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // 2 Finals 2016-11-2 添加图片数组
        ArrayList<Uri> imageUris = new ArrayList<Uri>();
        int count = 0;
        if (datas.size() > 9) {
            count = 9;
        } else {
            count = datas.size();
        }
        for (int i = 0; i < count; i++) {
            imageUris.add(Uri.fromFile(new File(datas.get(i))));
        }
        shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
        shareIntent.setType("image/*");
        // 3 Finals 2016-11-2 指定选择微信。
        ComponentName mComponentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
        shareIntent.setComponent(mComponentName);

        // 4 Finals 2016-11-2 开始分享。
        activity.startActivity(Intent.createChooser(shareIntent, "分享图片"));
    }

    /**
     * 直接分享文本到微信好友
     *
     * @param context 上下文
     */
    public void shareWechatFriend(Context context, String content) {
        if (CustomAppcation.isWeChatAppInstalled()) {
            Intent intent = new Intent();
            ComponentName cop = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
            intent.setComponent(cop);
            intent.setAction(Intent.ACTION_SEND);
            intent.putExtra("android.intent.extra.TEXT", content);
//            intent.putExtra("sms_body", content);
            intent.putExtra("Kdescription", !TextUtils.isEmpty(content) ? content : "");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else {
            ToastUtil.show("您需要安装微信客户端");
        }
    }

    /**
     * 直接分享纯文本内容至QQ好友
     *
     * @param mContext
     * @param content
     */
    public static void shareQQ(Context mContext, String content) {
        if (PlatformUtil.isInstallApp(mContext, PlatformUtil.PACKAGE_MOBILE_QQ)) {
            Intent intent = new Intent("android.intent.action.SEND");
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_SUBJECT, "share");
            intent.putExtra(Intent.EXTRA_TEXT, content);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity"));
            mContext.startActivity(intent);
        } else {
            ToastUtil.show("您需要安装QQ客户端");
        }

    }

}
